﻿#include "UPointTable.h"
#include "Global.h"
#include "UToolBarManager.h"
#include <qdebug.h>
UPointTable::UPointTable(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	ui.tableWidget->viewport()->installEventFilter(this);
	ui.tableWidget->verticalHeader()->setDefaultSectionSize(30);
	ui.tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
	ui.tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
	ui.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
	ui.tableWidget->setAttribute(Qt::WA_TransparentForMouseEvents, false);
	// 自动拉伸列宽
	ui.tableWidget->horizontalHeader()->setStretchLastSection(true);
	//table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	ui.tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); // ✅ 可以拖动
	ui.tableWidget->verticalHeader()->setVisible(false);
	// 选中整行
	ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui.tableWidget->setColumnCount(8);
	ui.tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
	ui.tableWidget->setHorizontalHeaderLabels({ A2T("测点名称"),  A2T("测点地址"),  A2T("测点描述"),  A2T("数值类型"),  A2T("当前值"), A2T("测点品质"), A2T("单位"), A2T("时间") });
	ui.tableWidget->setColumnWidth(0, 160);
	ui.tableWidget->setColumnWidth(1, 160);
	ui.tableWidget->setColumnWidth(2, 200);
	ui.tableWidget->setColumnWidth(3, 120);
	ui.tableWidget->setColumnWidth(4, 120);
	ui.tableWidget->setColumnWidth(5, 120);
	ui.tableWidget->setColumnWidth(6, 220);

	m_pTimer= new QTimer(this);
	connect(m_pTimer, &QTimer::timeout, this, &UPointTable::slot_upDate);
	startRefush();
}

UPointTable::~UPointTable()
{
	if (m_pTimer->isActive())
	{
		m_pTimer->stop();
	}
}
void UPointTable::setTablePoint(QList<CMeasurePoint*> pVecPoint)
{
	m_pVecPointList = pVecPoint;
	showPointToTable();
}

void UPointTable::startRefush()
{
	if (!m_pTimer->isActive())
	{
		m_pTimer->start(1000);
	}
}

void UPointTable::stopRefush()
{
	m_pVecPointList.clear();
	showPointToTable();
	if (m_pTimer->isActive())
	{
		m_pTimer->stop();
	}
}

bool UPointTable::eventFilter(QObject* obj, QEvent* event)
{
	if (obj == ui.tableWidget->viewport())
	{
		if (event->type() == QEvent::MouseButtonPress)
		{
			g_pToolBarMang->setToolBtn(UToolBarManager::Device, 2);
		}
	}
	return QWidget::eventFilter(obj, event);
}

void UPointTable::mouseReleaseEvent(QMouseEvent* event)
{
	g_pToolBarMang->setToolBtn(UToolBarManager::Device, 2);
	QWidget::mouseReleaseEvent(event);
}

void UPointTable::setTablePoint(QVector<CMeasurePoint*> pVecPoint)
{
	m_pVecPoint = pVecPoint;
	showPointToTable();
}

void UPointTable::showPointToTable()
{
	QTableWidget* table = ui.tableWidget;
	//table->clear();
	table->setRowCount(0);
	for (int i = 0; i < m_pVecPointList.size(); i++)
	{
		int row = table->rowCount();
		table->insertRow(row);
		table->setItem(row, 0, new QTableWidgetItem(m_pVecPointList[i]->getName()));
		table->setItem(row, 1, new QTableWidgetItem(m_pVecPointList[i]->getId()));
		table->setItem(row, 2, new QTableWidgetItem(m_pVecPointList[i]->getName()));
		table->setItem(row, 3, new QTableWidgetItem(m_pVecPointList[i]->getValType()));
		table->setItem(row, 4, new QTableWidgetItem(QString::number(m_pVecPointList[i]->getValue().toDouble(), 'f', 2)));
		table->setItem(row, 5, new QTableWidgetItem(m_pVecPointList[i]->getStatusInfo()));
		table->setItem(row, 6, new QTableWidgetItem(m_pVecPointList[i]->getUnit()));
		table->setItem(row, 7, new QTableWidgetItem(m_pVecPointList[i]->getLastUpdateTime().toString("yyyy-MM-dd hh:mm:ss")));
		QTableWidgetItem* item0 = ui.tableWidget->item(row, 0);
		QTableWidgetItem* item1 = ui.tableWidget->item(row, 1);
		QTableWidgetItem* item2 = ui.tableWidget->item(row, 2);
		QTableWidgetItem* item3 = ui.tableWidget->item(row, 3);
		QTableWidgetItem* item4 = ui.tableWidget->item(row, 4);
		QTableWidgetItem* item5 = ui.tableWidget->item(row, 5);
		QTableWidgetItem* item6 = ui.tableWidget->item(row, 6);
		QTableWidgetItem* item7 = ui.tableWidget->item(row, 7);
		item0->setTextAlignment(Qt::AlignCenter);
		item1->setTextAlignment(Qt::AlignCenter);
		item2->setTextAlignment(Qt::AlignCenter);
		item3->setTextAlignment(Qt::AlignCenter);
		item4->setTextAlignment(Qt::AlignCenter);
		item5->setTextAlignment(Qt::AlignCenter);
		item6->setTextAlignment(Qt::AlignCenter);
		item7->setTextAlignment(Qt::AlignCenter);
	}
}

void UPointTable::slot_upDate()
{
	if (!m_pTimer->isActive())
		return; 
	for (int i = 0; i < m_pVecPointList.size(); i++)
	{
		ui.tableWidget->item(i, 4)->setText(QString::number(m_pVecPointList[i]->getValue().toDouble(), 'f', 2));
		ui.tableWidget->item(i, 7)->setText(m_pVecPointList[i]->getLastUpdateTime().toString("yyyy-MM-dd hh:mm:ss"));
	}
}
